package com.zm.spring.jdbc;

import com.zm.spring.jdbc.Bean.Account;
import com.zm.spring.jdbc.Bean.OpRecord;
import com.zm.spring.jdbc.dao.AccountDao;
import com.zm.spring.jdbc.dao.OpRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BankBiz {
    @Autowired
    AccountDao accountDao;

    @Autowired
    OpRecordDao opRecordDao;

    @Autowired
    BankBiz1 bankBiz1;
    public void deposit(int aid,double money){
        Account account = accountDao.selectById(aid);
        account.setBalance(account.getBalance() + money);
        System.out.println("account = " + account);
        accountDao.updateById(account);
        OpRecord record = new OpRecord();
        record.setAid(aid);
        record.setOpmoney(money);
        record.setRemark("存款");
        opRecordDao.insert(record);
    }

    public void withdraw(int aid,double money) throws BizException {
        Account account = accountDao.selectById(aid);
        if (account.getBalance()<money){
            throw new BizException("余额不足");
        }
        account.setBalance(account.getBalance() - money);
        accountDao.updateById(account);
        OpRecord record = new OpRecord();
        record.setAid(aid);
        record.setOpmoney(-money);
        record.setRemark("取款");
        opRecordDao.insert(record);
    }

    public void transfer(int aid1,int aid2,double money) throws BizException {
        withdraw(aid1,money);
//        bankBiz1.deposit(aid2,money);
        deposit(aid2,money);
    }
}
